using System.Collections.Generic;
using System.Linq;
using RFS.BizLogic.PaiShi.Accounting;
using RFS.Domain.PaiShi;
using RFS.Domain.PaiShi.Accounting;

namespace RFS.BizLogic.PaiShi.Ports.Adapters
{
    public static class ExObjectEntityConverter
    {
        // public static AvsRepoEntity ToEntity(this IDayBasedWorkFlowNodeAccountingVoucherSetWrapper o)
        // {
        //     return null;
        //     // return new AvsRepoEntity()
        //     // {
        //     //     
        //     // }
        // }


        public static ProductEntity ToEntity(this IProduct o)
        {
            if (o == null) return new ProductEntity();

            return new ProductEntity()
            {
                Name = o.Name,
                Length = o.Length,
                Supplier = o.Supplier,
            };
        }

        public static IProduct ToProductDo(this ProductEntity o)
        {
            return new Product(o.Supplier, o.Name, o.Length);
        }


        public static OrderItemEntity ToEntity(this IOrderItem o)
        {
            return new OrderItemEntity()
            {
                // Id = o.Id,
                LocalId = o.LocalId,
                BizId = o.BizId,
                Quantity = o.Quantity,
                BizTime = o.BizTime,
                Amount = o.Amount,
                BizIdMapingName = o.BizIdMapingName,
                IsChecked = o.IsChecked,
                Product = o.Product.ToEntity(),
                RecordedInAcc = o.RecordedInAcc,
                OrdType = o.OrdType,
            };
        }

        public static IOrderItem ToDo(this OrderItemEntity o)
        {
            return new OrderItem()
            {
                // Id = o.Id,
                LocalId = o.LocalId,
                BizId = o.BizId,
                Quantity = o.Quantity,
                BizTime = o.BizTime,
                Amount = o.Amount,
                BizIdMapingName = o.BizIdMapingName,
                IsChecked = o.IsChecked,
                Product = o.Product.ToProductDo(),
                RecordedInAcc = o.RecordedInAcc,
                OrdType = o.OrdType,
            };
        }


        public static AccItemEntity ToEntity(this IOrderBasedAccItem o)
        {
            return new AccItemEntity()
            {
                Summary = o.Summary,
                Account = o.Account,
                RelatedOrderItem = o.RelatedOrderItem.ToEntity(),
                AmountDebit = o.AmountDebit,
                AmountCredit = o.AmountCredit,
                Id = o.Id,
            };
        }

        public static IOrderBasedAccItem ToDo(this AccItemEntity o)
        {
            return new OrderBasedAccItem()
            {
                Summary = o.Summary,
                Account = o.Account,
                RelatedOrderItem = o.RelatedOrderItem.ToDo(),
                AmountDebit = o.AmountDebit,
                AmountCredit = o.AmountCredit,
                AmountLeft = o.AmountLeft,
                Id = o.Id,
            };
        }
        
        
        
        public static AVEntity ToEntity(this IOrderItemBasedAccountingVoucher o)
        {
            return new AVEntity()
            {
                Id = o.Id,
                AccItems = o.AccItems.Select(x=> x.ToEntity()).ToList() ,
                VType = o.VType,
                
            };
        }

        public static IOrderItemBasedAccountingVoucher ToDo(this AVEntity o)
        {
            return new OrderItemBasedAccountingVoucher()
            {
                Id = o.Id,
                AccItems = o.AccItems.Select(x=> x.ToDo()).ToList() ,
                VType = o.VType,
            };
        }
        
                
        public static AVSEntity ToEntity(this IDayBasedWorkFlowNodeAccountingVoucherSetWrapper o)
        {
            return new AVSEntity()
            {
                Id = o.Id,
                Vouchers = o.Vouchers.Select(x=> x.ToEntity()).ToList() ,
                AccPeriod = o.AccPeriod,
                
            };
        }
        
              
        
        public static IEnumerable<AccItemAndOrderBasedDicItem> ToAccItemAndOrderBasedDicItem(this IDayBasedWorkFlowNodeAccountingVoucherSetWrapper o)
        {
            var ls = new List<AccItemAndOrderBasedDicItem>();
            
            foreach (var av in o.Vouchers)
            {
                foreach (var ai in av.AccItems)
                {
                    var y = ai;
                    var x = av;
                    var r = new AccItemAndOrderBasedDicItem()
                    {
                        BeginAt = o.AccPeriod.BeginDate,
                        EndAt = o.AccPeriod.EndDate,
                        AVSId = o.Id,
                        AvId = x.Id,
                        AccItemId = y.Id,
                    };
                    if (y.RelatedOrderItem is IOrderItem ord)
                    {
                        r.LocalId = (ulong)y.RelatedOrderItem.LocalId;
                        r.BizOrderId = ulong.Parse(y.RelatedOrderItem.BizId);
                        r.BizOrderType = y.RelatedOrderItem.OrdType;
                    }
                    r.IsChecked = av.IsBalanced;
                    ls.Add(r);  
                }
            }
            return ls;
        }
        
        
       
        public static IEnumerable<AccItemAndOrderBasedDicItem> ToAccItemAndOrderBasedDicItem(this AVSEntity o)
        {
            var ls = new List<AccItemAndOrderBasedDicItem>();
            
            foreach (var av in o.Vouchers)
            {
                var balance =  av.AccItems.Select(x=>x.AmountDebit).Sum()-  av.AccItems.Select(x=>x.AmountCredit).Sum() ;
                foreach (var ai in av.AccItems)
                {
                    var y = ai;
                    var x = av;
                    var r = new AccItemAndOrderBasedDicItem()
                    {
                        BeginAt = o.AccPeriod.BeginDate,
                        EndAt = o.AccPeriod.EndDate,
                        AVSId = o.Id,
                        AvId = x.Id,
                        AccItemId = y.Id,
                    };
                    if (y.RelatedOrderItem is OrderItemEntity ord)
                    {
                        r.LocalId = (ulong)y.RelatedOrderItem.LocalId;
                        r.BizOrderId = ulong.Parse(y.RelatedOrderItem.BizId);
                        r.BizOrderType = y.RelatedOrderItem.OrdType;
                    }
                    r.IsChecked = balance.AmountCash < (decimal)0.0001 ;
                    ls.Add(r);  
                }
            }
            return ls;
        }
        
        
        
        

        public static IDayBasedWorkFlowNodeAccountingVoucherSetWrapper ToDo(this AVSEntity o)
        {
            return new DayBasedWorkFlowNodeAccountingVoucherSetWrapper()
            {

                Id = o.Id,
                Vouchers = o.Vouchers.Select(x=> x.ToDo()).ToList() ,
                AccPeriod = o.AccPeriod,

            };
        }
        
        public static AVSListItemEntity ToListItem(this IDayBasedWorkFlowNodeAccountingVoucherSetWrapper o)
        {
            return new AVSListItemEntity()
            {
                Id = o.Id,
                AccPeriod = o.AccPeriod,
                TotalBalance = o.Balance,
                DebitAmount = o.DebitTotal ,
                CreditAmout = o.CreditTotal  ,

            };
        }
        
        
        
        
    }
}